package controller

import (
	"mini-app/config/mysql"
	"mini-app/internal/app/web/service"
	"mini-app/internal/dao"
	"mini-app/internal/models"

	"192.168.1.75/go-pkg/response"
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
)

type UserAddress struct{}

func (UserAddress) Create(c *gin.Context) {
	var params models.UserAddress
	c.ShouldBindBodyWith(&params, binding.JSON)
	id, err := service.UserAddress{}.Create(
		c.Request.Context(),
		c.GetString("openid"),
		params,
	)
	response.JSON(c, gin.H{
		"id": id,
	}, err)
}

func (UserAddress) Delete(c *gin.Context) {
	var params struct {
		ID string `json:"id"`
	}
	c.ShouldBindBodyWith(&params, binding.JSON)
	err := service.UserAddress{}.Delete(
		c.Request.Context(),
		c.GetString("openid"),
		params.ID,
	)
	response.JSON(c, nil, err)
}

func (UserAddress) Update(c *gin.Context) {
	var params models.UserAddress
	c.ShouldBindBodyWith(&params, binding.JSON)
	err := service.UserAddress{}.Update(
		c.Request.Context(),
		c.GetString("openid"),
		params,
	)
	response.JSON(c, nil, err)
}

func (UserAddress) List(c *gin.Context) {
	list := service.UserAddress{}.List(c.Request.Context(), c.GetString("openid"))
	response.JSON(c, gin.H{
		"list":  list,
		"total": len(list),
	}, nil)
}

func (UserAddress) SetDefault(c *gin.Context) {
	var params struct {
		ID string `json:"id"`
	}
	c.ShouldBindBodyWith(&params, binding.JSON)
	dao.UserAddress{}.SetDefault(c.Request.Context(), mysql.NewDB(), c.GetString("openid"), params.ID)
	response.JSON(c, nil, nil)
}
